package com.example.controller;

import com.example.common.Result;
import com.example.entity.ServerInfo;
import com.example.entity.ServerMonitorData;
import com.example.entity.ServerDailyStats;
import com.example.service.ServerMonitorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 服务器监控Controller
 */
@Tag(name = "监控接口")
@RestController
@RequestMapping("/api/monitor")
@RequiredArgsConstructor
@SecurityRequirement(name = "bearerAuth")
public class ServerMonitorController {
    
    private final ServerMonitorService serverMonitorService;
    
    @Operation(summary = "获取服务器信息")
    @GetMapping("/server/{id}")
    @PreAuthorize("isAuthenticated()")
    public Result<ServerInfo> getServerInfo(@PathVariable Long id) {
        return Result.success(serverMonitorService.getServerInfo(id));
    }
    
    @Operation(summary = "保存服务器信息")
    @PostMapping("/server")
    @PreAuthorize("isAuthenticated()")
    public Result<Void> saveServerInfo(@RequestBody ServerInfo serverInfo) {
        serverMonitorService.saveServerInfo(serverInfo);
        return Result.success(null);
    }
    
    @Operation(summary = "获取监控数据")
    @GetMapping("/data/{serverId}")
    @PreAuthorize("isAuthenticated()")
    public Result<List<ServerMonitorData>> getMonitorData(
            @PathVariable Long serverId,
            @RequestParam LocalDateTime startTime,
            @RequestParam LocalDateTime endTime) {
        return Result.success(serverMonitorService.getMonitorData(serverId, startTime, endTime));
    }
    
    @Operation(summary = "获取每日统计数据")
    @GetMapping("/stats/{serverId}")
    @PreAuthorize("isAuthenticated()")
    public Result<List<ServerDailyStats>> getDailyStats(
            @PathVariable Long serverId,
            @RequestParam LocalDateTime startTime,
            @RequestParam LocalDateTime endTime) {
        return Result.success(serverMonitorService.getDailyStats(serverId, startTime, endTime));
    }

    /**
     * 获取本机监控数据（最近一小时）
     */
    @Operation(summary = "获取本机最近监控数据")
    @GetMapping("/local")
    @PreAuthorize("isAuthenticated()")
    public Result<List<ServerMonitorData>> getLocalMonitorData() {
        // 获取最近一小时的数据
        LocalDateTime endTime = LocalDateTime.now();
        LocalDateTime startTime = endTime.minusHours(1);
        Long localServerId = serverMonitorService.getLocalServerId();
        return Result.success(serverMonitorService.getMonitorData(localServerId, startTime, endTime));
    }

    /**
     * 获取本机每日统计数据（最近7天）
     */
    @Operation(summary = "获取本机每日统计数据")
    @GetMapping("/local/stats")
    @PreAuthorize("isAuthenticated()")
    public Result<List<ServerDailyStats>> getLocalDailyStats() {
        // 获取最近7天的统计数据
        LocalDateTime endTime = LocalDateTime.now();
        LocalDateTime startTime = endTime.minusDays(7);
        Long localServerId = serverMonitorService.getLocalServerId();
        return Result.success(serverMonitorService.getDailyStats(localServerId, startTime, endTime));
    }

    /**
     * 获取本机服务器信息
     */
    @Operation(summary = "获取本机服务器信息")
    @GetMapping("/local/info")
    @PreAuthorize("isAuthenticated()")
    public Result<ServerInfo> getLocalServerInfo() {
        Long localServerId = serverMonitorService.getLocalServerId();
        return Result.success(serverMonitorService.getServerInfo(localServerId));
    }
} 